﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using iBeauty.BusinessObjects;
using iBeauty.DAL;

namespace iBeauty.BLL
{
    public class CustomerBLL
    {
        private CustomerDAO _CustomerDAO;

        public CustomerDAO CustomerDAO
        {
            get { return _CustomerDAO; }
            set { _CustomerDAO = value; }
        }



        public CustomerBLL()
        {
            CustomerDAO = new CustomerDAO();
        }
        public bool CheckExitsCustomer(string customerCode)
        {
            try
            {
                return CustomerDAO.CheckExitsCustomer(customerCode);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
      
        //TODO: Mã tự tăng
        public  string GetLastID()
        {
            try
            {
                return CustomerDAO.GetLastID();
            }
            catch (Exception ex)
            {
                
                throw ex;
            }
        }
        public string NextID(string lastID, string prefixID)
        {
            try
            {
                if (lastID == "")
                {
                    return prefixID + "0001";  // fixwidth default
                }
                int nextID = int.Parse(lastID.Remove(0, prefixID.Length)) + 1;
                int lengthNumerID = lastID.Length - prefixID.Length;
                string zeroNumber = "";
                for (int i = 1; i <= lengthNumerID; i++)
                {
                    if (nextID < Math.Pow(10, i))
                    {
                        for (int j = 1; j <= lengthNumerID - i; i++)
                        {
                            zeroNumber += "0";
                        }
                        return prefixID + zeroNumber + nextID.ToString();
                    }
                }
                return prefixID + nextID;
            }

            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<Customer> GetCustomers()
        {
            try
            {
                return CustomerDAO.GetCustomers();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public Customer GetCustomer(string ID)
        {
            try
            {
                return CustomerDAO.GetCustomer(ID);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public int AddCustomer(Customer oCustomer)
        {
            try
            {
                return CustomerDAO.AddCustomer(oCustomer);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public int AddCustomer(string ID, DateTime? CreateDate, DateTime? ModifiedDate, DateTime? TimeStamp, string CustomerCode, string CustomerName, string Barcode, string Email, string Address, string Phone, string Pictures, DateTime? Born, string Gender, int? Job, string Nationlaty, string BloodType, DateTime? FirstVisit, DateTime? LastVisit, int? AccountId, string Description)
        {
            try
            {
                return CustomerDAO.AddCustomer(ID, CreateDate, ModifiedDate, TimeStamp, CustomerCode, CustomerName, Barcode, Email, Address, Phone, Pictures, Born, Gender, Job, Nationlaty, BloodType, FirstVisit, LastVisit, AccountId, Description);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public int UpdateCustomer(Customer oCustomer)
        {
            try
            {
                return CustomerDAO.UpdateCustomer(oCustomer);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public int UpdateCustomer(string ID, DateTime? CreateDate, DateTime? ModifiedDate, DateTime? TimeStamp, string CustomerCode, string CustomerName, string Barcode, string Email, string Address, string Phone, string Pictures, DateTime? Born, string Gender, int? Job, string Nationlaty, string BloodType, DateTime? FirstVisit, DateTime? LastVisit, int? AccountId, string Description)
        {
            try
            {
                return CustomerDAO.UpdateCustomer(ID, CreateDate, ModifiedDate, TimeStamp, CustomerCode, CustomerName, Barcode, Email, Address, Phone, Pictures, Born, Gender, Job, Nationlaty, BloodType, FirstVisit, LastVisit, AccountId, Description);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public int RemoveCustomer(Customer oCustomer)
        {
            try
            {
                return CustomerDAO.RemoveCustomer(oCustomer);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public int RemoveCustomer(string ID)
        {
            try
            {
                return CustomerDAO.RemoveCustomer(ID);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public List<Customer> DeserializeCustomers(string Path)
        {
            try
            {
                return GenericXmlSerializer<List<Customer>>.Deserialize(Path);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void SerializeCustomers(string Path, List<Customer> Customers)
        {
            try
            {
                GenericXmlSerializer<List<Customer>>.Serialize(Customers, Path);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
